import math

LTE_MAX_NSYMB        = 7

LTE_MAX_PRB          = 110
LTE_NRE              = 12

LTE_SYMBOL_SZ_MAX    = 2048

LTE_CP_NORM_NSYMB    = 7
LTE_CP_NORM_SF_NSYMB = (2 * LTE_CP_NORM_NSYMB)
LTE_CP_NORM_0_LEN    = 160
LTE_CP_NORM_LEN      = 144

LTE_CP_EXT_NSYMB     = 6
LTE_CP_EXT_SF_NSYMB  = (2 * LTE_CP_EXT_NSYMB)
LTE_CP_EXT_LEN       = 512
LTE_CP_EXT_7_5_LEN   = 1024

LTE_NOF_SF_X_FRAME   = 10
LTE_NOF_SLOTS_PER_SF = 2
LTE_NSLOTS_X_FRAME   = (LTE_NOF_SLOTS_PER_SF * LTE_NOF_SF_X_FRAME)
def LTE_SF_LEN(symbol_sz):
    return symbol_sz * 15
LTE_SF_LEN_MAX = LTE_SF_LEN(LTE_SYMBOL_SZ_MAX)

def LTE_CP_NSYMB(cp):
    return LTE_CP_NORM_NSYMB if cp=="normal" else LTE_CP_EXT_NSYMB

def refsignal_cs_nsymbol(l, cp, port_id):
    if port_id < 2:
        if l % 2:
            return (l // 2 + 1) * LTE_CP_NSYMB(cp) - 3
        else:
            return (l // 2) * LTE_CP_NSYMB(cp)
    else:
        return 1 + l * LTE_CP_NSYMB(cp)

def refsignal_cs_nof_symbols(port_id):
    return 4 if port_id<2 else 2

def LTE_REFSIGNAL_PILOT_IDX(i, l, nof_prb):
    return 2 * nof_prb * l + i

def LTE_RE_IDX(nof_prb, symbol_idx, sample_idx):
    return symbol_idx * nof_prb * LTE_NRE + sample_idx

def refsignal_cs_v(port_id, ref_symbol_idx):
    v = 0
    if port_id==0:
        if ref_symbol_idx%2 == 0:
            v = 0
        else:
            v = 3
    elif port_id==1:
        if ref_symbol_idx%2 == 0:
            v = 3
        else:
            v = 0
    elif port_id==2:
        if ref_symbol_idx%2 == 0:
            v = 0
        else:
            v = 3
    elif port_id==3:
        if ref_symbol_idx%2 == 0:
            v = 3
        else:
            v = 0
    return v
def LTE_CP_LEN(symbol_sz, c):
    return int(math.ceil(c * symbol_sz / 2048.0))

def LTE_CP_LEN_NORM(symbol, symbol_sz):                                                                          \
    return LTE_CP_LEN(symbol_sz, LTE_CP_NORM_0_LEN) if symbol == 0 else LTE_CP_LEN(symbol_sz, LTE_CP_NORM_LEN)

def LTE_CP_LEN_EXT(symbol_sz):
    return LTE_CP_LEN(symbol_sz, LTE_CP_EXT_LEN)

LTE_SYMBOL_SZ = { 6:128 }